
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/guides/assumptions.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:23:59 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Assumptions &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../_static/sympy-notailtext-favicon.ico"/>
    <link href="assumptions.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Symbolic and fuzzy booleans" href="booleans.html" />
    <link rel="prev" title="SymPy Guides" href="index.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="booleans.html" title="Symbolic and fuzzy booleans"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="index.html" title="SymPy Guides"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">SymPy Guides</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Assumptions</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="assumptions">
<span id="id1"></span><h1>Assumptions<a class="headerlink" href="#assumptions" title="Permalink to this headline">¶</a></h1>
<p>This page outlines the core assumptions system in SymPy. It explains what the
core assumptions system is, how the assumptions system is used and what the
different assumptions predicates mean.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This page describes the core assumptions system also often referred
to as the “old assumptions” system. There is also a “new assumptions”
system which is described elsewhere. Note that the system described here is
actually the system that is widely used in SymPy. The “new assumptions”
system is not really used anywhere in SymPy yet and the “old assumptions”
system will not be removed. At the time of writing (SymPy 1.7) it is
still recommended for users to use the old assumption system.</p>
</div>
<p>Firstly we consider what happens when taking the square root of the square of
a concrete integer such as <span class="math notranslate nohighlight">\(2\)</span> or <span class="math notranslate nohighlight">\(-2\)</span>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt</span><span class="p">((</span><span class="o">-</span><span class="mi">2</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span> <span class="o">==</span> <span class="n">x</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="o">-</span><span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt</span><span class="p">(</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span> <span class="o">==</span> <span class="n">y</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt</span><span class="p">(</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="n">y</span>
<span class="go">True</span>
</pre></div>
</div>
<p>What these examples demonstrate is that for a positive number <span class="math notranslate nohighlight">\(x\)</span> we have
<span class="math notranslate nohighlight">\(\sqrt{x^2} = x\)</span> whereas for a negative number we would instead have
<span class="math notranslate nohighlight">\(\sqrt{x^2} = -x\)</span>. That may seem obvious but the situation can be more
surprising when working with a symbol rather then an explicit number. For
example</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Symbol</span><span class="p">,</span> <span class="n">simplify</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">sqrt(x**2)</span>
</pre></div>
</div>
<p>It might look as if that should simplify to <code class="docutils literal notranslate"><span class="pre">x</span></code> but it does not even if
<a class="reference internal" href="../modules/simplify/simplify.html#sympy.simplify.simplify.simplify" title="sympy.simplify.simplify.simplify"><code class="xref py py-func docutils literal notranslate"><span class="pre">simplify()</span></code></a> is used:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">simplify</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
<span class="go">sqrt(x**2)</span>
</pre></div>
</div>
<p>This is because SymPy will refuse to simplify this expression if the
simplification is not valid for <em>every</em> possible value of <code class="docutils literal notranslate"><span class="pre">x</span></code>. By default
the symbol <code class="docutils literal notranslate"><span class="pre">x</span></code> is considered only to represent something roughly like an
arbitrary complex number and the obvious simplification here is only valid for
positive real numbers. Since <code class="docutils literal notranslate"><span class="pre">x</span></code> is not known to be positive or even real no
simplification of this expression is possible.</p>
<p>We can tell SymPy that a symbol represents a positive real number when
creating the symbol and then the simplification will happen automatically:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt</span><span class="p">(</span><span class="n">y</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">y</span>
</pre></div>
</div>
<p>This is what is meant by “assumptions” in SymPy. If the symbol <code class="docutils literal notranslate"><span class="pre">y</span></code>
is created with <code class="docutils literal notranslate"><span class="pre">positive=True</span></code> then SymPy will <em>assume</em> that it represents
a positive real number rather than an arbitrary complex or possibly infinite
number. That <em>assumption</em> can make it possible to simplify expressions or
might allow other manipulations to work. It is usually a good idea to be as
precise as possible about the assumptions on a symbol when creating it.</p>
<section id="the-old-assumptions-system">
<h2>The (old) assumptions system<a class="headerlink" href="#the-old-assumptions-system" title="Permalink to this headline">¶</a></h2>
<p>There are two sides to the assumptions system. The first side is that we can
declare assumptions on a symbol when creating the symbol. The other side is
that we can query the assumptions on any expression using the corresponding
<code class="docutils literal notranslate"><span class="pre">is_*</span></code> attribute. For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">is_positive</span>
<span class="go">True</span>
</pre></div>
</div>
<p>We can query assumptions on any expression not just a symbol:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">x**2 + 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">is_positive</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">is_negative</span>
<span class="go">False</span>
</pre></div>
</div>
<p>The values given in an assumptions query use three-valued “fuzzy” logic. Any
query can return <code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code>, or <code class="docutils literal notranslate"><span class="pre">None</span></code> where <code class="docutils literal notranslate"><span class="pre">None</span></code> should be
interpreted as meaning that the result is <em>unknown</em>.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;z&#39;</span><span class="p">,</span> <span class="n">negative</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_positive</span><span class="p">)</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">is_positive</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">z</span><span class="o">.</span><span class="n">is_positive</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>We need to use <code class="docutils literal notranslate"><span class="pre">print</span></code> in the above examples because the special
value <code class="docutils literal notranslate"><span class="pre">None</span></code> does not display by default in the Python
interpretter.</p>
</div>
<p>There are several reasons why an assumptions query might give <code class="docutils literal notranslate"><span class="pre">None</span></code>. It is
possible that the query is <em>unknowable</em> as in the case of <code class="docutils literal notranslate"><span class="pre">x</span></code> above. Since
<code class="docutils literal notranslate"><span class="pre">x</span></code> does not have any assumptions declared it roughly represents an
arbitrary complex number. An arbitrary complex number <em>might</em> be a positive
real number but it also might <em>not</em> be. Without further information there is
no way to resolve the query <code class="docutils literal notranslate"><span class="pre">x.is_positive</span></code>.</p>
<p>Another reason why an assumptions query might give <code class="docutils literal notranslate"><span class="pre">None</span></code> is that there does
in many cases the problem of determining whether an expression is e.g.
positive is <em>undecidable</em>. That means that there does not exist an algorithm
for answering the query in general. For some cases an algorithm or at least a
simple check would be possible but has not yet been implemented although it
could be added to SymPy.</p>
<p>The final reason that an assumptions query might give <code class="docutils literal notranslate"><span class="pre">None</span></code> is just that
the assumptions system does not try very hard to answer complicated queries.
The system is intended to be fast and uses simple heuristic methods to
conclude a <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code> answer in common cases. For example any sum
of positive terms is positive so:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">symbols</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x, y&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">x + y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">is_positive</span>
<span class="go">True</span>
</pre></div>
</div>
<p>The last example is particularly simple so the assumptions system is able to
give a definite answer. If the sum involved a mix of positive or negative
terms it would be a harder query:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="mi">2</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">(x - 2)**2 + 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">is_positive</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr2</span> <span class="o">=</span> <span class="n">expr</span><span class="o">.</span><span class="n">expand</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr2</span>
<span class="go">x**2 - 4*x + 5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">expr2</span><span class="o">.</span><span class="n">is_positive</span><span class="p">)</span>
<span class="go">None</span>
</pre></div>
</div>
<p>Ideally that last example would give <code class="docutils literal notranslate"><span class="pre">True</span></code> rather than <code class="docutils literal notranslate"><span class="pre">None</span></code> because the
expression is always positive for any real value of <code class="docutils literal notranslate"><span class="pre">x</span></code> (and <code class="docutils literal notranslate"><span class="pre">x</span></code> has been
assumed real). The assumptions system is intended to be efficient though: it
is expected many more complex queries will not be fully resolved. This is
because assumptions queries are primarily used internally by SymPy as part of
low-level calculations. Making the system more comprehensive would slow SymPy
down.</p>
<p>Note that in fuzzy logic giving an indeterminate result <code class="docutils literal notranslate"><span class="pre">None</span></code> is never a
contradiction. If it is possible to infer a definite <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code>
result when resolving a query then that is better than returning <code class="docutils literal notranslate"><span class="pre">None</span></code>.
However a result of <code class="docutils literal notranslate"><span class="pre">None</span></code> is not a <em>bug</em>. Any code that uses the
assumptions system needs to be prepared to handle all three cases for any
query and should not presume that a definite answer will always be given.</p>
<p>The assumptions system is not just for symbols or for complex expressions. It
can also be used for plain SymPy integers and other objects. The assumptions
predicates are available on any instance of <a class="reference internal" href="../modules/core.html#sympy.core.basic.Basic" title="sympy.core.basic.Basic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Basic</span></code></a> which is the superclass
for most classes of SymPy objects. A plain Python <code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code> is not a
<a class="reference internal" href="../modules/core.html#sympy.core.basic.Basic" title="sympy.core.basic.Basic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Basic</span></code></a> instance and can not be used to query assumptions
predicates. We can “sympify” regular Python objects to become SymPy objects
with <a class="reference internal" href="../modules/core.html#sympy.core.sympify.sympify" title="sympy.core.sympify.sympify"><code class="xref py py-func docutils literal notranslate"><span class="pre">sympify()</span></code></a> or <code class="docutils literal notranslate"><span class="pre">S</span></code> (<a class="reference internal" href="../modules/core.html#sympy.core.singleton.SingletonRegistry" title="sympy.core.singleton.SingletonRegistry"><code class="xref py py-class docutils literal notranslate"><span class="pre">SingletonRegistry</span></code></a>) and then the
assumptions system can be used:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">S</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">is_positive</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">AttributeError</span>: <span class="n">&#39;int&#39; object has no attribute &#39;is_positive&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">S</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">&lt;class &#39;sympy.core.numbers.Integer&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">is_positive</span>
<span class="go">True</span>
</pre></div>
</div>
</section>
<section id="gotcha-symbols-with-different-assumptions">
<h2>Gotcha: symbols with different assumptions<a class="headerlink" href="#gotcha-symbols-with-different-assumptions" title="Permalink to this headline">¶</a></h2>
<p>In SymPy it is possible to declare two symbols with different names and they
will implicitly be considered equal under <em>structural equality</em>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x1</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x2</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x1</span>
<span class="go">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x2</span>
<span class="go">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x1</span> <span class="o">==</span> <span class="n">x2</span>
<span class="go">True</span>
</pre></div>
</div>
<p>However if the symbols have different assumptions then they will be considered
to represent distinct symbols:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x1</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x2</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x1</span>
<span class="go">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x2</span>
<span class="go">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x1</span> <span class="o">==</span> <span class="n">x2</span>
<span class="go">False</span>
</pre></div>
</div>
<p>One way to simplify an expression is to use the <a class="reference internal" href="../modules/simplify/simplify.html#sympy.simplify.simplify.posify" title="sympy.simplify.simplify.posify"><code class="xref py py-func docutils literal notranslate"><span class="pre">posify()</span></code></a> function
which will replace all symbols in an expression with symbols that have the
assumption <code class="docutils literal notranslate"><span class="pre">positive=True</span></code> (unless that contradicts any existing assumptions
for the symbol):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">posify</span><span class="p">,</span> <span class="n">exp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="n">exp</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span>
<span class="go">exp(sqrt(x**2))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">posify</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go">(exp(_x), {_x: x})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr2</span><span class="p">,</span> <span class="n">rep</span> <span class="o">=</span> <span class="n">posify</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr2</span>
<span class="go">exp(_x)</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../modules/simplify/simplify.html#sympy.simplify.simplify.posify" title="sympy.simplify.simplify.posify"><code class="xref py py-func docutils literal notranslate"><span class="pre">posify()</span></code></a> function returns the expression with all symbols replaced
(which might lead to simplifications) and also a dict which maps the new
symbols to the old that can be used with <a class="reference internal" href="../modules/core.html#sympy.core.basic.Basic.subs" title="sympy.core.basic.Basic.subs"><code class="xref py py-meth docutils literal notranslate"><span class="pre">subs()</span></code></a>. This is
useful because otherwise the new expression with the new symbols having the
<code class="docutils literal notranslate"><span class="pre">positive=True</span></code> assumption will not compare equal to the old:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expr2</span>
<span class="go">exp(_x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr2</span> <span class="o">==</span> <span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr2</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">rep</span><span class="p">)</span>
<span class="go">exp(x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr2</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">rep</span><span class="p">)</span> <span class="o">==</span> <span class="n">exp</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
</section>
<section id="applying-assumptions-to-string-inputs">
<h2>Applying assumptions to string inputs<a class="headerlink" href="#applying-assumptions-to-string-inputs" title="Permalink to this headline">¶</a></h2>
<p>We have seen how to set assumptions when  <a class="reference internal" href="../modules/core.html#sympy.core.symbol.Symbol" title="sympy.core.symbol.Symbol"><code class="xref py py-class docutils literal notranslate"><span class="pre">Symbol</span></code></a> or
<a class="reference internal" href="../modules/core.html#sympy.core.symbol.symbols" title="sympy.core.symbol.symbols"><code class="xref py py-func docutils literal notranslate"><span class="pre">symbols()</span></code></a> explicitly. A natural question to ask is in what other
situations can we assign assumptions to an object?</p>
<p>It is common for users to use strings as input to SymPy functions (although
the general feeling among SymPy developers is that this should be discouraged)
e.g.:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">solve</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">solve</span><span class="p">(</span><span class="s1">&#39;x**2 - 1&#39;</span><span class="p">)</span>
<span class="go">[-1, 1]</span>
</pre></div>
</div>
<p>When creating symbols explicitly it would be possible to assign assumptions
that would affect the behaviour of <a class="reference internal" href="../modules/solvers/solvers.html#sympy.solvers.solvers.solve" title="sympy.solvers.solvers.solve"><code class="xref py py-func docutils literal notranslate"><span class="pre">solve()</span></code></a>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">solve</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">[1]</span>
</pre></div>
</div>
<p>When using string input SymPy will create the expression and create all of the
symbolc implicitly so the question arises how can the assumptions be
specified? The answer is that rather than depending on implicit string
conversion it is better to use the <a class="reference internal" href="../modules/parsing.html#sympy.parsing.sympy_parser.parse_expr" title="sympy.parsing.sympy_parser.parse_expr"><code class="xref py py-func docutils literal notranslate"><span class="pre">parse_expr()</span></code></a> function explicitly
and then it is possible to provide assumptions for the symbols e.g.:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">parse_expr</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">parse_expr</span><span class="p">(</span><span class="s1">&#39;x**2 - 1&#39;</span><span class="p">)</span>
<span class="go">x**2 - 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">eq</span> <span class="o">=</span> <span class="n">parse_expr</span><span class="p">(</span><span class="s1">&#39;x**2 - 1&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;x&#39;</span><span class="p">:</span><span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">solve</span><span class="p">(</span><span class="n">eq</span><span class="p">)</span>
<span class="go">[1]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The <a class="reference internal" href="../modules/solvers/solvers.html#sympy.solvers.solvers.solve" title="sympy.solvers.solvers.solve"><code class="xref py py-func docutils literal notranslate"><span class="pre">solve()</span></code></a> function is unusual as a high level API in that it
actually checks the assumptions on any input symbols (the unknowns)
and uses that to tailor its output. The assumptions system otherwise
affects low-level evaluation but is not necessarily handled
explicitly by high-level APIs.</p>
</div>
</section>
<section id="predicates">
<h2>Predicates<a class="headerlink" href="#predicates" title="Permalink to this headline">¶</a></h2>
<p>There are many different predicates that can be assumed for a symbol or can be
queried for an expression. It is possible to combine multiple predicates when
creating a symbol. Predicates are logically combined using <em>and</em> so if a
symbol is declared with <code class="docutils literal notranslate"><span class="pre">positive=True</span></code> and also with <code class="docutils literal notranslate"><span class="pre">integer=True</span></code> then
it is both positive <em>and</em> integer:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">is_positive</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">is_integer</span>
<span class="go">True</span>
</pre></div>
</div>
<p>The full set of known predicates for a symbol can be accessed using the
<a class="reference internal" href="../modules/core.html#sympy.core.basic.Basic.assumptions0" title="sympy.core.basic.Basic.assumptions0"><code class="xref py py-attr docutils literal notranslate"><span class="pre">assumptions0</span></code></a> attribute:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">assumptions0</span>
<span class="go">{&#39;algebraic&#39;: True,</span>
<span class="go"> &#39;commutative&#39;: True,</span>
<span class="go"> &#39;complex&#39;: True,</span>
<span class="go"> &#39;extended_negative&#39;: False,</span>
<span class="go"> &#39;extended_nonnegative&#39;: True,</span>
<span class="go"> &#39;extended_nonpositive&#39;: False,</span>
<span class="go"> &#39;extended_nonzero&#39;: True,</span>
<span class="go"> &#39;extended_positive&#39;: True,</span>
<span class="go"> &#39;extended_real&#39;: True,</span>
<span class="go"> &#39;finite&#39;: True,</span>
<span class="go"> &#39;hermitian&#39;: True,</span>
<span class="go"> &#39;imaginary&#39;: False,</span>
<span class="go"> &#39;infinite&#39;: False,</span>
<span class="go"> &#39;integer&#39;: True,</span>
<span class="go"> &#39;irrational&#39;: False,</span>
<span class="go"> &#39;negative&#39;: False,</span>
<span class="go"> &#39;noninteger&#39;: False,</span>
<span class="go"> &#39;nonnegative&#39;: True,</span>
<span class="go"> &#39;nonpositive&#39;: False,</span>
<span class="go"> &#39;nonzero&#39;: True,</span>
<span class="go"> &#39;positive&#39;: True,</span>
<span class="go"> &#39;rational&#39;: True,</span>
<span class="go"> &#39;real&#39;: True,</span>
<span class="go"> &#39;transcendental&#39;: False,</span>
<span class="go"> &#39;zero&#39;: False}</span>
</pre></div>
</div>
<p>We can see that there are many more predicates listed than the two that were
used to create <code class="docutils literal notranslate"><span class="pre">x</span></code>. This is because the assumptions system can infer some
predicates from combinations of other predicates. For example if a symbol is
declared with <code class="docutils literal notranslate"><span class="pre">positive=True</span></code> then it is possible to infer that it should
have <code class="docutils literal notranslate"><span class="pre">negative=False</span></code> because a positive number can never be negative.
Similarly if a symbol is created with <code class="docutils literal notranslate"><span class="pre">integer=True</span></code> then it is possible to
infer that is should have <code class="docutils literal notranslate"><span class="pre">rational=True</span></code> because every integer is a
rational number.</p>
<p>A full table of the possible predicates and their definitions is given below.</p>
<table class="colwidths-given docutils align-default" id="id29">
<caption><span class="caption-text">Assumptions predicates for the (old) assumptions</span><a class="headerlink" href="#id29" title="Permalink to this table">¶</a></caption>
<colgroup>
<col style="width: 20%" />
<col style="width: 45%" />
<col style="width: 35%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Predicate</p></th>
<th class="head"><p>Definition</p></th>
<th class="head"><p>Implications</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">commutative</span></code></p></td>
<td><p>A commutative expression. A <code class="docutils literal notranslate"><span class="pre">commutative</span></code> expression commutes with
all other expressions under multiplication. If an expression <code class="docutils literal notranslate"><span class="pre">a</span></code> has
<code class="docutils literal notranslate"><span class="pre">commutative=True</span></code> then <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">*</span> <span class="pre">b</span> <span class="pre">==</span> <span class="pre">b</span> <span class="pre">*</span> <span class="pre">a</span></code> for any other expression
<code class="docutils literal notranslate"><span class="pre">b</span></code> (even if <code class="docutils literal notranslate"><span class="pre">b</span></code> is not <code class="docutils literal notranslate"><span class="pre">commutative</span></code>).  Unlike all other
assumptions predicates <code class="docutils literal notranslate"><span class="pre">commutative</span></code> must always be <code class="docutils literal notranslate"><span class="pre">True</span></code> or
<code class="docutils literal notranslate"><span class="pre">False</span></code> and can never be <code class="docutils literal notranslate"><span class="pre">None</span></code>. Also unlike all other predicates
<code class="docutils literal notranslate"><span class="pre">commutative</span></code> defaults to <code class="docutils literal notranslate"><span class="pre">True</span></code> in e.g.  <code class="docutils literal notranslate"><span class="pre">Symbol('x')</span></code>.
<a class="reference internal" href="#commutative" id="id2"><span>[commutative]</span></a></p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">infinite</span></code></p></td>
<td><p>An infinite expression such as <code class="docutils literal notranslate"><span class="pre">oo</span></code>, <code class="docutils literal notranslate"><span class="pre">-oo</span></code> or <code class="docutils literal notranslate"><span class="pre">zoo</span></code>.
<a class="reference internal" href="#infinite" id="id3"><span>[infinite]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">!finite</span></code></div>
</div>
</td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">finite</span></code></p></td>
<td><p>A finite expression. Any expression that is not <code class="docutils literal notranslate"><span class="pre">infinite</span></code> is
considered <code class="docutils literal notranslate"><span class="pre">finite</span></code>.
<a class="reference internal" href="#infinite" id="id4"><span>[infinite]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">!infinite</span></code></div>
</div>
</td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">hermitian</span></code></p></td>
<td><p>An element of the field of Hermitian operators.
<a class="reference internal" href="#antihermitian" id="id5"><span>[antihermitian]</span></a></p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">antihermitian</span></code></p></td>
<td><p>An element of the field of antihermitian operators.
<a class="reference internal" href="#antihermitian" id="id6"><span>[antihermitian]</span></a></p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">complex</span></code></p></td>
<td><p>A complex number, <span class="math notranslate nohighlight">\(z\in\mathbb{C}\)</span>. Any number of the form <span class="math notranslate nohighlight">\(x + iy\)</span>
where <span class="math notranslate nohighlight">\(x\)</span> and <span class="math notranslate nohighlight">\(y\)</span> are <code class="docutils literal notranslate"><span class="pre">real</span></code> and <span class="math notranslate nohighlight">\(i = \sqrt{-1}\)</span>. All <code class="docutils literal notranslate"><span class="pre">complex</span></code>
numbers are <code class="docutils literal notranslate"><span class="pre">finite</span></code>. Includes all <code class="docutils literal notranslate"><span class="pre">real</span></code> numbers.
<a class="reference internal" href="#complex" id="id7"><span>[complex]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">commutative</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">finite</span></code></div>
</div>
</td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">algebraic</span></code></p></td>
<td><p>An algebraic number, <span class="math notranslate nohighlight">\(z\in\overline{\mathbb{Q}}\)</span>. Any number that is a root
of a non-zero polynomial <span class="math notranslate nohighlight">\(p(z)\in\mathbb{Q}[z]\)</span> having rational
coefficients. All <code class="docutils literal notranslate"><span class="pre">algebraic</span></code> numbers are <code class="docutils literal notranslate"><span class="pre">complex</span></code>. An
<code class="docutils literal notranslate"><span class="pre">algebraic</span></code> number may or may not be <code class="docutils literal notranslate"><span class="pre">real</span></code>. Includes all
<code class="docutils literal notranslate"><span class="pre">rational</span></code> numbers.
<a class="reference internal" href="#algebraic" id="id8"><span>[algebraic]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">complex</span></code></div>
</div>
</td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">transcendental</span></code></p></td>
<td><p>A complex number that is not algebraic,
<span class="math notranslate nohighlight">\(z\in\mathbb{C}-\overline{\mathbb{Q}}\)</span>. All <code class="docutils literal notranslate"><span class="pre">transcendental</span></code> numbers are
<code class="docutils literal notranslate"><span class="pre">complex</span></code>. A <code class="docutils literal notranslate"><span class="pre">transcendental</span></code> number may or may not be <code class="docutils literal notranslate"><span class="pre">real</span></code>
but can never be <code class="docutils literal notranslate"><span class="pre">rational</span></code>.
<a class="reference internal" href="#transcendental" id="id9"><span>[transcendental]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(complex</span> <span class="pre">&amp;</span> <span class="pre">!algebraic)</span></code></div>
</div>
</td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">extended_real</span></code></p></td>
<td><p>An element of the extended real number line,
<span class="math notranslate nohighlight">\(x\in\overline{\mathbb{R}}\)</span> where
<span class="math notranslate nohighlight">\(\overline{\mathbb{R}}=\mathbb{R}\cup\{-\infty,+\infty\}\)</span>. An
<code class="docutils literal notranslate"><span class="pre">extended_real</span></code> number is either <code class="docutils literal notranslate"><span class="pre">real</span></code> or <span class="math notranslate nohighlight">\(\pm\infty\)</span>. The
relational operators <code class="docutils literal notranslate"><span class="pre">&lt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;=</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;=</span></code> and <code class="docutils literal notranslate"><span class="pre">&gt;</span></code> are defined only
for expressions that are <code class="docutils literal notranslate"><span class="pre">extended_real</span></code>.
<a class="reference internal" href="#extended-real" id="id10"><span>[extended_real]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">commutative</span></code></div>
</div>
</td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">real</span></code></p></td>
<td><p>A real number, <span class="math notranslate nohighlight">\(x\in\mathbb{R}\)</span>. All <code class="docutils literal notranslate"><span class="pre">real</span></code> numbers are <code class="docutils literal notranslate"><span class="pre">finite</span></code>
and <code class="docutils literal notranslate"><span class="pre">complex</span></code> (the set of reals is a subset of the set of complex
numbers).  Includes all <code class="docutils literal notranslate"><span class="pre">rational</span></code> numbers. A <code class="docutils literal notranslate"><span class="pre">real</span></code> number is
either <code class="docutils literal notranslate"><span class="pre">negative</span></code>, <code class="docutils literal notranslate"><span class="pre">zero</span></code> or <code class="docutils literal notranslate"><span class="pre">positive</span></code>.
<a class="reference internal" href="#real" id="id11"><span>[real]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">complex</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(extended_real</span> <span class="pre">&amp;</span> <span class="pre">finite)</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(negative</span> <span class="pre">|</span> <span class="pre">zero</span> <span class="pre">|</span> <span class="pre">positive)</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">hermitian</span></code></div>
</div>
</td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">imaginary</span></code></p></td>
<td><p>An imaginary number, <span class="math notranslate nohighlight">\(z\in\mathbb{I}-\{0\}\)</span>. A number of the form <span class="math notranslate nohighlight">\(z=yi\)</span>
where <span class="math notranslate nohighlight">\(y\)</span> is real, <span class="math notranslate nohighlight">\(y\ne 0\)</span> and <span class="math notranslate nohighlight">\(i=\sqrt{-1}\)</span>. All <code class="docutils literal notranslate"><span class="pre">imaginary</span></code>
numbers are <code class="docutils literal notranslate"><span class="pre">complex</span></code> and not <code class="docutils literal notranslate"><span class="pre">real</span></code>. Note in particular that
<code class="docutils literal notranslate"><span class="pre">zero</span></code> is <span class="math notranslate nohighlight">\(not\)</span> considered <code class="docutils literal notranslate"><span class="pre">imaginary</span></code> in SymPy.
<a class="reference internal" href="#imaginary" id="id12"><span>[imaginary]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">complex</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">antihermitian</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">!extended_real</span></code></div>
</div>
</td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">rational</span></code></p></td>
<td><p>A rational number, <span class="math notranslate nohighlight">\(q\in\mathbb{Q}\)</span>. Any number of the form
<span class="math notranslate nohighlight">\(\frac{a}{b}\)</span> where <span class="math notranslate nohighlight">\(a\)</span> and <span class="math notranslate nohighlight">\(b\)</span> are integers and <span class="math notranslate nohighlight">\(b \ne 0\)</span>. All
<code class="docutils literal notranslate"><span class="pre">rational</span></code> numbers are <code class="docutils literal notranslate"><span class="pre">real</span></code> and <code class="docutils literal notranslate"><span class="pre">algebraic</span></code>.  Includes all
<code class="docutils literal notranslate"><span class="pre">integer</span></code> numbers.
<a class="reference internal" href="#rational" id="id13"><span>[rational]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">real</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">algebraic</span></code></div>
</div>
</td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">irrational</span></code></p></td>
<td><p>A real number that is not rational, <span class="math notranslate nohighlight">\(x\in\mathbb{R}-\mathbb{Q}\)</span>.
<a class="reference internal" href="#irrational" id="id14"><span>[irrational]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(real</span> <span class="pre">&amp;</span> <span class="pre">!rational)</span></code></div>
</div>
</td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">integer</span></code></p></td>
<td><p>An integer, <span class="math notranslate nohighlight">\(a\in\mathbb{Z}\)</span>. All integers are <code class="docutils literal notranslate"><span class="pre">rational</span></code>.  Includes
<code class="docutils literal notranslate"><span class="pre">zero</span></code> and all <code class="docutils literal notranslate"><span class="pre">prime</span></code>, <code class="docutils literal notranslate"><span class="pre">composite</span></code>, <code class="docutils literal notranslate"><span class="pre">even</span></code> and <code class="docutils literal notranslate"><span class="pre">odd</span></code> numbers.
<a class="reference internal" href="#integer" id="id15"><span>[integer]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">rational</span></code></div>
</div>
</td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">noninteger</span></code></p></td>
<td><p>An extended real number that is not an integer,
<span class="math notranslate nohighlight">\(x\in\overline{\mathbb{R}}-\mathbb{Z}\)</span>.</p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(extended_real</span> <span class="pre">&amp;</span> <span class="pre">!integer)</span></code></div>
</div>
</td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">even</span></code></p></td>
<td><p>An even number, <span class="math notranslate nohighlight">\(e\in\{2k: k\in\mathbb{Z}\}\)</span>. All <code class="docutils literal notranslate"><span class="pre">even</span></code> numbers are
<code class="docutils literal notranslate"><span class="pre">integer</span></code> numbers. Includes <code class="docutils literal notranslate"><span class="pre">zero</span></code>.
<a class="reference internal" href="#parity" id="id16"><span>[parity]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">integer</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">!odd</span></code></div>
</div>
</td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">odd</span></code></p></td>
<td><p>An odd number, <span class="math notranslate nohighlight">\(o\in\{2k + 1: k\in\mathbb{Z}\}\)</span>. All <code class="docutils literal notranslate"><span class="pre">odd</span></code> numbers are
<code class="docutils literal notranslate"><span class="pre">integer</span></code> numbers.
<a class="reference internal" href="#parity" id="id17"><span>[parity]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">integer</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">!even</span></code></div>
</div>
</td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">prime</span></code></p></td>
<td><p>A prime number, <span class="math notranslate nohighlight">\(p\in\mathbb{P}\)</span>. All <code class="docutils literal notranslate"><span class="pre">prime</span></code> numbers are
<code class="docutils literal notranslate"><span class="pre">positive</span></code> and <code class="docutils literal notranslate"><span class="pre">integer</span></code>.
<a class="reference internal" href="#prime" id="id18"><span>[prime]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">integer</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">positive</span></code></div>
</div>
</td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">composite</span></code></p></td>
<td><p>A composite number, <span class="math notranslate nohighlight">\(c\in\mathbb{N}-(\mathbb{P}\cup\{1\})\)</span>. A positive
integer that is the product of two or more primes. A <code class="docutils literal notranslate"><span class="pre">composite</span></code>
number is always a <code class="docutils literal notranslate"><span class="pre">positive</span></code> <code class="docutils literal notranslate"><span class="pre">integer</span></code> and is not <code class="docutils literal notranslate"><span class="pre">prime</span></code>.
<a class="reference internal" href="#composite" id="id19"><span>[composite]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">(integer</span> <span class="pre">&amp;</span> <span class="pre">positive</span> <span class="pre">&amp;</span> <span class="pre">!prime)</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">!composite</span> <span class="pre">-&gt;</span> <span class="pre">(!positive</span> <span class="pre">|</span> <span class="pre">!even</span> <span class="pre">|</span> <span class="pre">prime)</span></code></div>
</div>
</td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">zero</span></code></p></td>
<td><p>The number <span class="math notranslate nohighlight">\(0\)</span>. An expression with <code class="docutils literal notranslate"><span class="pre">zero=True</span></code> represents the
number <code class="docutils literal notranslate"><span class="pre">0</span></code> which is an <code class="docutils literal notranslate"><span class="pre">integer</span></code>.
<a class="reference internal" href="#zero" id="id20"><span>[zero]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">even</span> <span class="pre">&amp;</span> <span class="pre">finite</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(extended_nonnegative</span> <span class="pre">&amp;</span> <span class="pre">extended_nonpositive)</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(nonnegative</span> <span class="pre">&amp;</span> <span class="pre">nonpositive)</span></code></div>
</div>
</td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">nonzero</span></code></p></td>
<td><p>A nonzero real number, <span class="math notranslate nohighlight">\(x\in\mathbb{R}-\{0\}\)</span>. A <code class="docutils literal notranslate"><span class="pre">nonzero</span></code> number
is always <code class="docutils literal notranslate"><span class="pre">real</span></code> and can not be <code class="docutils literal notranslate"><span class="pre">zero</span></code>.</p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">-&gt;</span> <span class="pre">real</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(extended_nonzero</span> <span class="pre">&amp;</span> <span class="pre">finite)</span></code></div>
</div>
</td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">extended_nonzero</span></code></p></td>
<td><p>A member of the extended reals that is not zero,
<span class="math notranslate nohighlight">\(x\in\overline{\mathbb{R}}-\{0\}\)</span>.</p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(extended_real</span> <span class="pre">&amp;</span> <span class="pre">!zero)</span></code></div>
</div>
</td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">positive</span></code></p></td>
<td><p>A positive real number, <span class="math notranslate nohighlight">\(x\in\mathbb{R}, x&gt;0\)</span>. All <code class="docutils literal notranslate"><span class="pre">positive</span></code>
numbers are <code class="docutils literal notranslate"><span class="pre">finite</span></code> so <code class="docutils literal notranslate"><span class="pre">oo</span></code> is not <code class="docutils literal notranslate"><span class="pre">positive</span></code>.
<a class="reference internal" href="#positive" id="id21"><span>[positive]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(nonnegative</span> <span class="pre">&amp;</span> <span class="pre">nonzero)</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(extended_positive</span> <span class="pre">&amp;</span> <span class="pre">finite)</span></code></div>
</div>
</td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">nonnegative</span></code></p></td>
<td><p>A nonnegative real number, <span class="math notranslate nohighlight">\(x\in\mathbb{R}, x\ge 0\)</span>. All <code class="docutils literal notranslate"><span class="pre">nonnegative</span></code>
numbers are <code class="docutils literal notranslate"><span class="pre">finite</span></code> so <code class="docutils literal notranslate"><span class="pre">oo</span></code> is not <code class="docutils literal notranslate"><span class="pre">nonnegative</span></code>.
<a class="reference internal" href="#positive" id="id22"><span>[positive]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(real</span> <span class="pre">&amp;</span> <span class="pre">!negative)</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(extended_nonnegative</span> <span class="pre">&amp;</span> <span class="pre">finite)</span></code></div>
</div>
</td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">negative</span></code></p></td>
<td><p>A negative real number, <span class="math notranslate nohighlight">\(x\in\mathbb{R}, x&lt;0\)</span>. All <code class="docutils literal notranslate"><span class="pre">negative</span></code>
numbers are <code class="docutils literal notranslate"><span class="pre">finite</span></code> so <code class="docutils literal notranslate"><span class="pre">-oo</span></code> is not <code class="docutils literal notranslate"><span class="pre">negative</span></code>.
<a class="reference internal" href="#negative" id="id23"><span>[negative]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(nonpositive</span> <span class="pre">&amp;</span> <span class="pre">nonzero)</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(extended_negative</span> <span class="pre">&amp;</span> <span class="pre">finite)</span></code></div>
</div>
</td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">nonpositive</span></code></p></td>
<td><p>A nonpositive real number, <span class="math notranslate nohighlight">\(x\in\mathbb{R}, x\le 0\)</span>. All <code class="docutils literal notranslate"><span class="pre">nonpositive</span></code>
numbers are <code class="docutils literal notranslate"><span class="pre">finite</span></code> so <code class="docutils literal notranslate"><span class="pre">-oo</span></code> is not <code class="docutils literal notranslate"><span class="pre">nonpositive</span></code>.
<a class="reference internal" href="#negative" id="id24"><span>[negative]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(real</span> <span class="pre">&amp;</span> <span class="pre">!positive)</span></code></div>
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(extended_nonpositive</span> <span class="pre">&amp;</span> <span class="pre">finite)</span></code></div>
</div>
</td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">extended_positive</span></code></p></td>
<td><p>A positive extended real number, <span class="math notranslate nohighlight">\(x\in\overline{\mathbb{R}}, x&gt;0\)</span>.
An <code class="docutils literal notranslate"><span class="pre">extended_positive</span></code> number is either <code class="docutils literal notranslate"><span class="pre">positive</span></code> or <code class="docutils literal notranslate"><span class="pre">oo</span></code>.
<a class="reference internal" href="#extended-real" id="id25"><span>[extended_real]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(extended_nonnegative</span> <span class="pre">&amp;</span> <span class="pre">extended_nonzero)</span></code></div>
</div>
</td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">extended_nonnegative</span></code></p></td>
<td><p>A nonnegative extended real number, <span class="math notranslate nohighlight">\(x\in\overline{\mathbb{R}}, x\ge 0\)</span>.
An <code class="docutils literal notranslate"><span class="pre">extended_nonnegative</span></code> number is either <code class="docutils literal notranslate"><span class="pre">nonnegative</span></code> or <code class="docutils literal notranslate"><span class="pre">oo</span></code>.
<a class="reference internal" href="#extended-real" id="id26"><span>[extended_real]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(extended_real</span> <span class="pre">&amp;</span> <span class="pre">!extended_negative)</span></code></div>
</div>
</td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">extended_negative</span></code></p></td>
<td><p>A negative extended real number, <span class="math notranslate nohighlight">\(x\in\overline{\mathbb{R}}, x&lt;0\)</span>.
An <code class="docutils literal notranslate"><span class="pre">extended_negative</span></code> number is either <code class="docutils literal notranslate"><span class="pre">negative</span></code> or <code class="docutils literal notranslate"><span class="pre">-oo</span></code>.
<a class="reference internal" href="#extended-real" id="id27"><span>[extended_real]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(extended_nonpositive</span> <span class="pre">&amp;</span> <span class="pre">extended_nonzero)</span></code></div>
</div>
</td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">extended_nonpositive</span></code></p></td>
<td><p>A nonpositive extended real number, <span class="math notranslate nohighlight">\(x\in\overline{\mathbb{R}}, x\le 0\)</span>.
An <code class="docutils literal notranslate"><span class="pre">extended_nonpositive</span></code> number is either <code class="docutils literal notranslate"><span class="pre">nonpositive</span></code> or <code class="docutils literal notranslate"><span class="pre">-oo</span></code>.
<a class="reference internal" href="#extended-real" id="id28"><span>[extended_real]</span></a></p></td>
<td><div class="line-block">
<div class="line"><code class="docutils literal notranslate"><span class="pre">==</span> <span class="pre">(extended_real</span> <span class="pre">&amp;</span> <span class="pre">!extended_positive)</span></code></div>
</div>
</td>
</tr>
</tbody>
</table>
<section id="references-for-the-above-definitions">
<h3>References for the above definitions<a class="headerlink" href="#references-for-the-above-definitions" title="Permalink to this headline">¶</a></h3>
<dl class="citation">
<dt class="label" id="commutative"><span class="brackets"><a class="fn-backref" href="#id2">commutative</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Commutative_property">https://en.wikipedia.org/wiki/Commutative_property</a></p>
</dd>
<dt class="label" id="infinite"><span class="brackets">infinite</span><span class="fn-backref">(<a href="#id3">1</a>,<a href="#id4">2</a>)</span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Infinity">https://en.wikipedia.org/wiki/Infinity</a></p>
</dd>
<dt class="label" id="antihermitian"><span class="brackets">antihermitian</span><span class="fn-backref">(<a href="#id5">1</a>,<a href="#id6">2</a>)</span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Skew-Hermitian_matrix">https://en.wikipedia.org/wiki/Skew-Hermitian_matrix</a></p>
</dd>
<dt class="label" id="complex"><span class="brackets"><a class="fn-backref" href="#id7">complex</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Complex_number">https://en.wikipedia.org/wiki/Complex_number</a></p>
</dd>
<dt class="label" id="algebraic"><span class="brackets"><a class="fn-backref" href="#id8">algebraic</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Algebraic_number">https://en.wikipedia.org/wiki/Algebraic_number</a></p>
</dd>
<dt class="label" id="transcendental"><span class="brackets"><a class="fn-backref" href="#id9">transcendental</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Transcendental_number">https://en.wikipedia.org/wiki/Transcendental_number</a></p>
</dd>
<dt class="label" id="extended-real"><span class="brackets">extended_real</span><span class="fn-backref">(<a href="#id10">1</a>,<a href="#id25">2</a>,<a href="#id26">3</a>,<a href="#id27">4</a>,<a href="#id28">5</a>)</span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Extended_real_number_line">https://en.wikipedia.org/wiki/Extended_real_number_line</a></p>
</dd>
<dt class="label" id="real"><span class="brackets"><a class="fn-backref" href="#id11">real</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Real_number">https://en.wikipedia.org/wiki/Real_number</a></p>
</dd>
<dt class="label" id="imaginary"><span class="brackets"><a class="fn-backref" href="#id12">imaginary</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Imaginary_number">https://en.wikipedia.org/wiki/Imaginary_number</a></p>
</dd>
<dt class="label" id="rational"><span class="brackets"><a class="fn-backref" href="#id13">rational</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Rational_number">https://en.wikipedia.org/wiki/Rational_number</a></p>
</dd>
<dt class="label" id="irrational"><span class="brackets"><a class="fn-backref" href="#id14">irrational</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Irrational_number">https://en.wikipedia.org/wiki/Irrational_number</a></p>
</dd>
<dt class="label" id="integer"><span class="brackets"><a class="fn-backref" href="#id15">integer</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Integer">https://en.wikipedia.org/wiki/Integer</a></p>
</dd>
<dt class="label" id="parity"><span class="brackets">parity</span><span class="fn-backref">(<a href="#id16">1</a>,<a href="#id17">2</a>)</span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Parity_(mathematics">https://en.wikipedia.org/wiki/Parity_(mathematics</a>)</p>
</dd>
<dt class="label" id="prime"><span class="brackets"><a class="fn-backref" href="#id18">prime</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Prime_number">https://en.wikipedia.org/wiki/Prime_number</a></p>
</dd>
<dt class="label" id="composite"><span class="brackets"><a class="fn-backref" href="#id19">composite</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Composite_number">https://en.wikipedia.org/wiki/Composite_number</a></p>
</dd>
<dt class="label" id="zero"><span class="brackets"><a class="fn-backref" href="#id20">zero</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/0">https://en.wikipedia.org/wiki/0</a></p>
</dd>
<dt class="label" id="positive"><span class="brackets">positive</span><span class="fn-backref">(<a href="#id21">1</a>,<a href="#id22">2</a>)</span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Positive_real_numbers">https://en.wikipedia.org/wiki/Positive_real_numbers</a></p>
</dd>
<dt class="label" id="negative"><span class="brackets">negative</span><span class="fn-backref">(<a href="#id23">1</a>,<a href="#id24">2</a>)</span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Negative_number">https://en.wikipedia.org/wiki/Negative_number</a></p>
</dd>
</dl>
</section>
</section>
<section id="implications">
<h2>Implications<a class="headerlink" href="#implications" title="Permalink to this headline">¶</a></h2>
<p>The assumptions system uses the inference rules to infer new predicates beyond
those immediately specified when creating a symbol:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">negative</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">zero</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">is_positive</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Although <code class="docutils literal notranslate"><span class="pre">x</span></code> was not explicitly declared <code class="docutils literal notranslate"><span class="pre">positive</span></code> it can be inferred
from the predicates that were given explicitly. Specifically one of the
inference rules is <code class="docutils literal notranslate"><span class="pre">real</span> <span class="pre">==</span> <span class="pre">negative</span> <span class="pre">|</span> <span class="pre">zero</span> <span class="pre">|</span> <span class="pre">positive</span></code> so if <code class="docutils literal notranslate"><span class="pre">real</span></code> is
<code class="docutils literal notranslate"><span class="pre">True</span></code> and both <code class="docutils literal notranslate"><span class="pre">negative</span></code> and <code class="docutils literal notranslate"><span class="pre">zero</span></code> are <code class="docutils literal notranslate"><span class="pre">False</span></code> then <code class="docutils literal notranslate"><span class="pre">positive</span></code>
must be <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
<p>In practice the assumption inference rules mean that it is not necessary to
include redundant predicates for example a positive real number can be simply
be declared as positive:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x1</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x1&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x2</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x2&#39;</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x1</span><span class="o">.</span><span class="n">is_real</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x2</span><span class="o">.</span><span class="n">is_real</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x1</span><span class="o">.</span><span class="n">assumptions0</span> <span class="o">==</span> <span class="n">x2</span><span class="o">.</span><span class="n">assumptions0</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Combining predicates that are inconsistent will give an error:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">commutative</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">InconsistentAssumptions</span>: <span class="n">{</span>
<span class="go">      algebraic: False,</span>
<span class="go">      commutative: False,</span>
<span class="go">      complex: False,</span>
<span class="go">      composite: False,</span>
<span class="go">      even: False,</span>
<span class="go">      extended_negative: False,</span>
<span class="go">      extended_nonnegative: False,</span>
<span class="go">      extended_nonpositive: False,</span>
<span class="go">      extended_nonzero: False,</span>
<span class="go">      extended_positive: False,</span>
<span class="go">      extended_real: False,</span>
<span class="go">      imaginary: False,</span>
<span class="go">      integer: False,</span>
<span class="go">      irrational: False,</span>
<span class="go">      negative: False,</span>
<span class="go">      noninteger: False,</span>
<span class="go">      nonnegative: False,</span>
<span class="go">      nonpositive: False,</span>
<span class="go">      nonzero: False,</span>
<span class="go">      odd: False,</span>
<span class="go">      positive: False,</span>
<span class="go">      prime: False,</span>
<span class="go">      rational: False,</span>
<span class="go">      real: False,</span>
<span class="go">      transcendental: False,</span>
<span class="go">      zero: False}, real=True</span>
</pre></div>
</div>
</section>
<section id="interpretation-of-the-predicates">
<h2>Interpretation of the predicates<a class="headerlink" href="#interpretation-of-the-predicates" title="Permalink to this headline">¶</a></h2>
<p>Although the predicates are defined in the table above it is worth taking some
time to think about how to interpret them. Firstly many of the concepts
referred to by the predicate names like “zero”, “prime”, “rational” etc have
a basic meaning in mathematics but can also have more general meanings. For
example when dealing with matrices a matrix of all zeros might be referred to
as “zero”. The predicates in the assumptions system do not allow any
generalizations such as this. The predicate <code class="docutils literal notranslate"><span class="pre">zero</span></code> is strictly reserved for
the plain number <span class="math notranslate nohighlight">\(0\)</span>. Instead matrices have an
<a class="reference internal" href="../modules/matrices/common.html#sympy.matrices.common.MatrixCommon.is_zero_matrix" title="sympy.matrices.common.MatrixCommon.is_zero_matrix"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_zero_matrix()</span></code></a> property for this purpose (although
that property is not strictly part of the assumptions system):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Matrix</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="n">Matrix</span><span class="p">([[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">.</span><span class="n">is_zero</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span><span class="o">.</span><span class="n">is_zero_matrix</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Similarly there are generalisations of the integers such as the Gaussian
integers which have a different notion of prime number. The <code class="docutils literal notranslate"><span class="pre">prime</span></code>
predicate in the assumptions system does not include those and strictly refers
only to the standard prime numbers <span class="math notranslate nohighlight">\(\mathbb{P} = \{2, 3, 5, 7, 11, \cdots\}\)</span>.
Likewise <code class="docutils literal notranslate"><span class="pre">integer</span></code> only means the standard concept of the integers
<span class="math notranslate nohighlight">\(\mathbb{Z} = \{0, \pm 1, \pm 2, \cdots\}\)</span>, <code class="docutils literal notranslate"><span class="pre">rational</span></code> only means the
standard concept of the rational numbers <span class="math notranslate nohighlight">\(\mathbb{Q}\)</span> and so on.</p>
<p>The predicates set up schemes of subsets such as the chain beginning with the
complex numbers which are considered as a superset of the reals which are in
turn a superset of the rationals and so on. The chain of subsets</p>
<div class="math notranslate nohighlight">
\[\mathbb{Z} \subset \mathbb{Q} \subset \mathbb{R} \subset \mathbb{C}\]</div>
<p>corresponds to the chain of implications in the assumptions system</p>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="n">integer</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">rational</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="n">real</span><span class="w"> </span><span class="o">-&gt;</span><span class="w"> </span><span class="k">complex</span><span class="w"></span>
</pre></div>
</div>
<p>A “vanilla” symbol with no assumptions explicitly attached is not known to
belong to any of these sets and is not even known to be finite:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">assumptions0</span>
<span class="go">{&#39;commutative&#39;: True}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_commutative</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_rational</span><span class="p">)</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_complex</span><span class="p">)</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_real</span><span class="p">)</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_integer</span><span class="p">)</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_finite</span><span class="p">)</span>
<span class="go">None</span>
</pre></div>
</div>
<p>It is hard for SymPy to know what it can do with such a symbol that is not
even known to be finite or complex so it is generally better to give some
assumptions to the symbol explicitly. Many parts of SymPy will implicitly
treat such a symbol as complex and in some cases SymPy will permit
manipulations that would not strictly be valid given that <code class="docutils literal notranslate"><span class="pre">x</span></code> is not known
to be finite. In a formal sense though very little is known about a vanilla
symbol which makes manipulations involving it difficult.</p>
<p>Defining <em>something</em> about a symbol can make a big difference. For example if
we declare the symbol to be an integer then this implies a suite of other
predicates that will help in further manipulations:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;n&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="o">.</span><span class="n">assumptions0</span>
<span class="go">{&#39;algebraic&#39;: True,</span>
<span class="go"> &#39;commutative&#39;: True,</span>
<span class="go"> &#39;complex&#39;: True,</span>
<span class="go"> &#39;extended_real&#39;: True,</span>
<span class="go"> &#39;finite&#39;: True,</span>
<span class="go"> &#39;hermitian&#39;: True,</span>
<span class="go"> &#39;imaginary&#39;: False,</span>
<span class="go"> &#39;infinite&#39;: False,</span>
<span class="go"> &#39;integer&#39;: True,</span>
<span class="go"> &#39;irrational&#39;: False,</span>
<span class="go"> &#39;noninteger&#39;: False,</span>
<span class="go"> &#39;rational&#39;: True,</span>
<span class="go"> &#39;real&#39;: True,</span>
<span class="go"> &#39;transcendental&#39;: False}</span>
</pre></div>
</div>
<p>These assumptions can lead to very significant simplifications e.g.
<code class="docutils literal notranslate"><span class="pre">integer=True</span></code> gives:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sin</span><span class="p">,</span> <span class="n">pi</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n1</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;n1&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n2</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;n2&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sin</span><span class="p">(</span><span class="n">n1</span> <span class="o">*</span> <span class="n">pi</span><span class="p">)</span>
<span class="go">sin(pi*n1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sin</span><span class="p">(</span><span class="n">n2</span> <span class="o">*</span> <span class="n">pi</span><span class="p">)</span>
<span class="go">0</span>
</pre></div>
</div>
<p>Replacing a whole expression with <span class="math notranslate nohighlight">\(0\)</span> is about as good as simplification can
get!</p>
<p>It is normally advisable to set as many assumptions as possible on any symbols
so that expressions can be simplified as much as possible. A common
misunderstanding leads to defining a symbol with a <code class="docutils literal notranslate"><span class="pre">False</span></code> predicate e.g.:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">negative</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_negative</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_nonnegative</span><span class="p">)</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_real</span><span class="p">)</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_complex</span><span class="p">)</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_finite</span><span class="p">)</span>
<span class="go">None</span>
</pre></div>
</div>
<p>If the intention is to say that <code class="docutils literal notranslate"><span class="pre">x</span></code> is a real number that is not positive
then that needs to be explicitly stated. In the context that the symbol is
known to be real, the predicate <code class="docutils literal notranslate"><span class="pre">positive=False</span></code> becomes much more
meaningful:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">negative</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_negative</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_nonnegative</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_real</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_complex</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_finite</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>A symbol declared as <code class="docutils literal notranslate"><span class="pre">Symbol('x',</span> <span class="pre">real=True,</span> <span class="pre">negative=False)</span></code> is equivalent
to a symbol declared as <code class="docutils literal notranslate"><span class="pre">Symbol('x',</span> <span class="pre">nonnegative=True)</span></code>. Simply declaring a
symbol as <code class="docutils literal notranslate"><span class="pre">Symbol('x',</span> <span class="pre">positive=False)</span></code> does not allow the assumptions
system to conclude much about it because a vanilla symbol is not known to be
finite or even complex.</p>
<p>A related confusion arises with <code class="docutils literal notranslate"><span class="pre">Symbol('x',</span> <span class="pre">complex=True)</span></code> and
<code class="docutils literal notranslate"><span class="pre">Symbol('x',</span> <span class="pre">real=False)</span></code>. Often when either of these is used neither is
what is actually wanted. The first thing to understand is that all real
numbers are complex so a symbol created with <code class="docutils literal notranslate"><span class="pre">real=True</span></code> will also have
<code class="docutils literal notranslate"><span class="pre">complex=True</span></code> and a symbol created with <code class="docutils literal notranslate"><span class="pre">complex=True</span></code> will not have
<code class="docutils literal notranslate"><span class="pre">real=False</span></code>. If the intention was to create a complex number that is not
a real number then it should be <code class="docutils literal notranslate"><span class="pre">Symbol('x',</span> <span class="pre">complex=True,</span> <span class="pre">real=False)</span></code>. On
the other hand declaring <code class="docutils literal notranslate"><span class="pre">real=False</span></code> alone is not sufficient to conclude
that <code class="docutils literal notranslate"><span class="pre">complex=True</span></code> because knowing that it is not a real number does not
tell us whether it is finite or whether or not it is some completely different
kind of object from a complex number.</p>
<p>A vanilla symbol is defined by not knowing whether it is <code class="docutils literal notranslate"><span class="pre">finite</span></code> etc but
there is no clear definition of what it <em>should</em> actually represent. It is
tempting to think of it as an “arbitrary complex number or possibly one of the
infinities” but there is no way to query an arbitrary (non-symbol) expression
in order to determine if it meets those criteria. It is important to bear in
mind that within the SymPy codebase and potentially in downstream libraries
many other kinds of mathematical objects can be found that might also have
<code class="docutils literal notranslate"><span class="pre">commutative=True</span></code> while being something very different from an ordinary
number (in this context even SymPy’s standard infinities are considered
“ordinary”).</p>
<p>The only predicate that is applied by default for a symbol is <code class="docutils literal notranslate"><span class="pre">commutative</span></code>.
We can also declare a symbol to be <em>noncommutative</em> e.g.:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x, y&#39;</span><span class="p">,</span> <span class="n">commutative</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;z&#39;</span><span class="p">)</span>  <span class="c1"># defaults to commutative=True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">*</span><span class="n">y</span> <span class="o">+</span> <span class="n">y</span><span class="o">*</span><span class="n">x</span>
<span class="go">x*y + y*x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">*</span><span class="n">z</span> <span class="o">+</span> <span class="n">z</span><span class="o">*</span><span class="n">x</span>
<span class="go">2*z*x</span>
</pre></div>
</div>
<p>Note here that since <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span></code> are both noncommutative <code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span></code>
do not commute so <code class="docutils literal notranslate"><span class="pre">x*y</span> <span class="pre">!=</span> <span class="pre">y*x</span></code>. On the other hand since <code class="docutils literal notranslate"><span class="pre">z</span></code> is commutative
<code class="docutils literal notranslate"><span class="pre">x</span></code> and <code class="docutils literal notranslate"><span class="pre">z</span></code> commute and <code class="docutils literal notranslate"><span class="pre">x*z</span> <span class="pre">==</span> <span class="pre">z*x</span></code> even though <code class="docutils literal notranslate"><span class="pre">x</span></code> is
noncommutative.</p>
<p>The interpretation of what a vanilla symbol represents is unclear but the
interpretation of an expression with <code class="docutils literal notranslate"><span class="pre">commutative=False</span></code> is entirely
obscure. Such an expression is necessarily not a complex number or an
extended real or any of the standard infinities (even <code class="docutils literal notranslate"><span class="pre">zoo</span></code> is commutative).
We are left with very little that we can say about what such an expression
<em>does</em> represent.</p>
</section>
<section id="other-is-properties">
<h2>Other is_* properties<a class="headerlink" href="#other-is-properties" title="Permalink to this headline">¶</a></h2>
<p>There are many properties and attributes in SymPy that that have names
beginning with <code class="docutils literal notranslate"><span class="pre">is_</span></code> that look similar to the properties used in the
(old) assumptions system but are not in fact part of the assumptions system.
Some of these have a similar meaning and usage as those of the assumptions
system such as the <a class="reference internal" href="../modules/matrices/common.html#sympy.matrices.common.MatrixCommon.is_zero_matrix" title="sympy.matrices.common.MatrixCommon.is_zero_matrix"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_zero_matrix()</span></code></a> property shown
above.  Another example is the <code class="docutils literal notranslate"><span class="pre">is_empty</span></code> property of sets:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">FiniteSet</span><span class="p">,</span> <span class="n">Intersection</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S1</span> <span class="o">=</span> <span class="n">FiniteSet</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S1</span>
<span class="go">{1, 2}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">S1</span><span class="o">.</span><span class="n">is_empty</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S2</span> <span class="o">=</span> <span class="n">Intersection</span><span class="p">(</span><span class="n">FiniteSet</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">FiniteSet</span><span class="p">(</span><span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S2</span>
<span class="go">Intersection({1}, {x})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">S2</span><span class="o">.</span><span class="n">is_empty</span><span class="p">)</span>
<span class="go">None</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">is_empty</span></code> property gives a fuzzy-bool indicating whether or not a
<a class="reference internal" href="../modules/sets.html#sympy.sets.sets.Set" title="sympy.sets.sets.Set"><code class="xref py py-class docutils literal notranslate"><span class="pre">Set</span></code></a> is the empty set. In the example of <code class="docutils literal notranslate"><span class="pre">S2</span></code> it is not possible to know
whether or not the set is empty without knowing whether or not <code class="docutils literal notranslate"><span class="pre">x</span></code> is equal
to <code class="docutils literal notranslate"><span class="pre">1</span></code> so <code class="docutils literal notranslate"><span class="pre">S2.is_empty</span></code> gives <code class="docutils literal notranslate"><span class="pre">None</span></code>. The <code class="docutils literal notranslate"><span class="pre">is_empty</span></code> property for sets
plays a similar role to the <code class="docutils literal notranslate"><span class="pre">is_zero</span></code> property for numbers in the
assumptions system: <code class="docutils literal notranslate"><span class="pre">is_empty</span></code> is normally only <code class="docutils literal notranslate"><span class="pre">True</span></code> for the
<a class="reference internal" href="../modules/sets.html#sympy.sets.sets.EmptySet" title="sympy.sets.sets.EmptySet"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmptySet</span></code></a> object but it is still useful to be able to distinguish between
the cases where <code class="docutils literal notranslate"><span class="pre">is_empty=False</span></code> and <code class="docutils literal notranslate"><span class="pre">is_empty=None</span></code>.</p>
<p>Although <code class="docutils literal notranslate"><span class="pre">is_zero_matrix</span></code> and <code class="docutils literal notranslate"><span class="pre">is_empty</span></code> are used for similar purposes to
the assumptions properties such as <code class="docutils literal notranslate"><span class="pre">is_zero</span></code> they are not part of the
(old) assumptions system. There are no associated inference rules connecting
e.g.  <code class="docutils literal notranslate"><span class="pre">Set.is_empty</span></code> and <code class="docutils literal notranslate"><span class="pre">Set.is_finite_set</span></code> because the inference rules
are part of the (old) assumptions system which only deals with the predicates
listed in the table above. It is not possible to declare a
<a class="reference internal" href="../modules/matrices/expressions.html#sympy.matrices.expressions.MatrixSymbol" title="sympy.matrices.expressions.MatrixSymbol"><code class="xref py py-class docutils literal notranslate"><span class="pre">MatrixSymbol</span></code></a> with e.g. <code class="docutils literal notranslate"><span class="pre">zero_matrix=False</span></code> and there is no
<code class="docutils literal notranslate"><span class="pre">SetSymbol</span></code> class but if there was it would not have a system for
understanding predicates like <code class="docutils literal notranslate"><span class="pre">empty=False</span></code>.</p>
<p>The properties <a class="reference internal" href="../modules/matrices/common.html#sympy.matrices.common.MatrixCommon.is_zero_matrix" title="sympy.matrices.common.MatrixCommon.is_zero_matrix"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_zero_matrix()</span></code></a> and <code class="docutils literal notranslate"><span class="pre">is_empty</span></code> are
similar to those of the assumptions system because they concern <em>semantic</em>
aspects of an expression. There are a large number of other properties that
focus on <em>structural</em> aspects such as <code class="docutils literal notranslate"><span class="pre">is_Number</span></code>,
<a class="reference internal" href="../modules/core.html#sympy.core.expr.Expr.is_number" title="sympy.core.expr.Expr.is_number"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_number()</span></code></a>, <a class="reference internal" href="../modules/core.html#sympy.core.basic.Basic.is_comparable" title="sympy.core.basic.Basic.is_comparable"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_comparable()</span></code></a>. Since these
properties refer to structural aspects of an expression they will always give
<code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code> rather than a fuzzy bool that also has the possibility
of being <code class="docutils literal notranslate"><span class="pre">None</span></code>. Capitalised properties such as <code class="docutils literal notranslate"><span class="pre">is_Number</span></code> are usually
shorthand for <code class="docutils literal notranslate"><span class="pre">isinstance</span></code> checks e.g.:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Number</span><span class="p">,</span> <span class="n">Rational</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Rational</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">Number</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">is_Number</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">,</span> <span class="n">rational</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">Number</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span><span class="o">.</span><span class="n">is_Number</span>
<span class="go">False</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../modules/core.html#sympy.core.numbers.Number" title="sympy.core.numbers.Number"><code class="xref py py-class docutils literal notranslate"><span class="pre">Number</span></code></a> class is the superclass for <a class="reference internal" href="../modules/core.html#sympy.core.numbers.Integer" title="sympy.core.numbers.Integer"><code class="xref py py-class docutils literal notranslate"><span class="pre">Integer</span></code></a>,
<a class="reference internal" href="../modules/core.html#sympy.core.numbers.Rational" title="sympy.core.numbers.Rational"><code class="xref py py-class docutils literal notranslate"><span class="pre">Rational</span></code></a> and <a class="reference internal" href="../modules/core.html#sympy.core.numbers.Float" title="sympy.core.numbers.Float"><code class="xref py py-class docutils literal notranslate"><span class="pre">Float</span></code></a> so any instance of <a class="reference internal" href="../modules/core.html#sympy.core.numbers.Number" title="sympy.core.numbers.Number"><code class="xref py py-class docutils literal notranslate"><span class="pre">Number</span></code></a>
represents a concrete number with a known value. A symbol such as <code class="docutils literal notranslate"><span class="pre">y</span></code> that
is declared with <code class="docutils literal notranslate"><span class="pre">rational=True</span></code> might represent the same value as <code class="docutils literal notranslate"><span class="pre">x</span></code> but
it is not a concrete number with a known value so this is a structural rather
than a semantic distinction.  Properties like <code class="docutils literal notranslate"><span class="pre">is_Number</span></code> are sometimes used
in SymPy in place of e.g. <code class="docutils literal notranslate"><span class="pre">isinstance(obj,</span> <span class="pre">Number)</span></code> because they do not have
problems with circular imports and checking <code class="docutils literal notranslate"><span class="pre">x.is_Number</span></code> can be faster than
a call to <code class="docutils literal notranslate"><span class="pre">isinstance</span></code>.</p>
<p>The <a class="reference internal" href="../modules/core.html#sympy.core.expr.Expr.is_number" title="sympy.core.expr.Expr.is_number"><code class="xref py py-attr docutils literal notranslate"><span class="pre">is_number</span></code></a> (lower-case) property is very different from
<code class="docutils literal notranslate"><span class="pre">is_Number</span></code>. The <a class="reference internal" href="../modules/core.html#sympy.core.expr.Expr.is_number" title="sympy.core.expr.Expr.is_number"><code class="xref py py-attr docutils literal notranslate"><span class="pre">is_number</span></code></a> property is <code class="docutils literal notranslate"><span class="pre">True</span></code> for any
expression that can be numerically evaluated to a floating point complex
number with <a class="reference internal" href="../modules/core.html#sympy.core.evalf.EvalfMixin.evalf" title="sympy.core.evalf.EvalfMixin.evalf"><code class="xref py py-meth docutils literal notranslate"><span class="pre">evalf()</span></code></a>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">I</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr1</span> <span class="o">=</span> <span class="n">I</span> <span class="o">+</span> <span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr1</span>
<span class="go">sqrt(2) + I</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr1</span><span class="o">.</span><span class="n">is_number</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr1</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">1.4142135623731 + 1.0*I</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr2</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr2</span>
<span class="go">x + 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr2</span><span class="o">.</span><span class="n">is_number</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr2</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
<span class="go">x + 1.0</span>
</pre></div>
</div>
<p>The primary reason for checking <code class="docutils literal notranslate"><span class="pre">expr.is_number</span></code> is to predict whether a
call to <a class="reference internal" href="../modules/core.html#sympy.core.evalf.EvalfMixin.evalf" title="sympy.core.evalf.EvalfMixin.evalf"><code class="xref py py-meth docutils literal notranslate"><span class="pre">evalf()</span></code></a> will fully evaluate. The
<a class="reference internal" href="../modules/core.html#sympy.core.basic.Basic.is_comparable" title="sympy.core.basic.Basic.is_comparable"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_comparable()</span></code></a> property is similar to
<a class="reference internal" href="../modules/core.html#sympy.core.expr.Expr.is_number" title="sympy.core.expr.Expr.is_number"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_number()</span></code></a> except that if <code class="docutils literal notranslate"><span class="pre">is_comparable</span></code> gives <code class="docutils literal notranslate"><span class="pre">True</span></code>
then the expression is guaranteed to numerically evaluate to a <em>real</em>
<a class="reference internal" href="../modules/core.html#sympy.core.numbers.Float" title="sympy.core.numbers.Float"><code class="xref py py-class docutils literal notranslate"><span class="pre">Float</span></code></a>.  When <code class="docutils literal notranslate"><span class="pre">a.is_comparable</span></code> and <code class="docutils literal notranslate"><span class="pre">b.is_comparable</span></code> the
inequality <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">&lt;</span> <span class="pre">b</span></code> should be resolvable as something like <code class="docutils literal notranslate"><span class="pre">a.evalf()</span> <span class="pre">&lt;</span>
<span class="pre">b.evalf()</span></code>.</p>
<p>The full set of <code class="docutils literal notranslate"><span class="pre">is_*</span></code> properties, attributes and methods in SymPy is
large. It is important to be clear though that only those that are listed in
the table of predicates above are actually part of the assumptions system. It
is only those properties that are involved in the <em>mechanism</em> that implements
the assumptions system which is explained below.</p>
</section>
<section id="implementing-assumptions-handlers">
<h2>Implementing assumptions handlers<a class="headerlink" href="#implementing-assumptions-handlers" title="Permalink to this headline">¶</a></h2>
<p>We will now work through an example of how to implement a SymPy symbolic
function so that we can see how the old assumptions are used internally. SymPy
already has an <code class="docutils literal notranslate"><span class="pre">exp</span></code> function which is defined for all complex numbers but
we will define an <code class="docutils literal notranslate"><span class="pre">expreal</span></code> function which is restricted to real arguments.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Function</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.core.logic</span> <span class="kn">import</span> <span class="n">fuzzy_and</span><span class="p">,</span> <span class="n">fuzzy_or</span>
<span class="go">&gt;&gt;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">expreal</span><span class="p">(</span><span class="n">Function</span><span class="p">):</span>
<span class="gp">... </span>    <span class="sd">&quot;&quot;&quot;exponential function E**x restricted to the extended reals&quot;&quot;&quot;</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="n">is_extended_nonnegative</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="nd">@classmethod</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">eval</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="gp">... </span>        <span class="c1"># Validate the argument</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">is_extended_real</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;non-real argument to expreal&quot;</span><span class="p">)</span>
<span class="gp">... </span>        <span class="c1"># Evaluate for special values</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">is_zero</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="n">S</span><span class="o">.</span><span class="n">One</span>
<span class="gp">... </span>        <span class="k">elif</span> <span class="n">x</span><span class="o">.</span><span class="n">is_infinite</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">is_extended_negative</span><span class="p">:</span>
<span class="gp">... </span>                <span class="k">return</span> <span class="n">S</span><span class="o">.</span><span class="n">Zero</span>
<span class="gp">... </span>            <span class="k">elif</span> <span class="n">x</span><span class="o">.</span><span class="n">is_extended_positive</span><span class="p">:</span>
<span class="gp">... </span>                <span class="k">return</span> <span class="n">S</span><span class="o">.</span><span class="n">Infinity</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="nd">@property</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">x</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">_eval_is_finite</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="n">fuzzy_or</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">is_real</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">is_extended_nonpositive</span><span class="p">])</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">_eval_is_algebraic</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="n">fuzzy_and</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">is_rational</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">is_nonzero</span><span class="p">]):</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="kc">False</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">_eval_is_integer</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">is_zero</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">return</span> <span class="kc">True</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">_eval_is_zero</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="n">fuzzy_and</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">is_infinite</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="o">.</span><span class="n">is_extended_negative</span><span class="p">])</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">Function.eval</span></code> method is used to pick up on special values of the function so
that we can return a different object if it would be a simplification. When
<code class="docutils literal notranslate"><span class="pre">expreal(x)</span></code> is called the <code class="docutils literal notranslate"><span class="pre">expreal.__new__</span></code> class method (defined in the
superclass <code class="docutils literal notranslate"><span class="pre">Function</span></code>) will call <code class="docutils literal notranslate"><span class="pre">expreal.eval(x)</span></code>. If <code class="docutils literal notranslate"><span class="pre">expreal.eval</span></code>
returns something other than <code class="docutils literal notranslate"><span class="pre">None</span></code> then that will be returned instead of an
unevaluated <code class="docutils literal notranslate"><span class="pre">expreal(x)</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">oo</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">expreal(1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="o">-</span><span class="n">oo</span><span class="p">)</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="n">oo</span><span class="p">)</span>
<span class="go">oo</span>
</pre></div>
</div>
<p>Note that the <code class="docutils literal notranslate"><span class="pre">expreal.eval</span></code> method does not compare the argument using
<code class="docutils literal notranslate"><span class="pre">==</span></code>. The special values are verified using the assumptions system to query
the properties of the argument. That means that the <code class="docutils literal notranslate"><span class="pre">expreal</span></code> method can
also evaluate for different forms of expression that have matching properties
e.g.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">extended_negative</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">infinite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span>
<span class="go">x</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">0</span>
</pre></div>
</div>
<p>Of course the assumptions system can only resolve a limited number of special
values so most <code class="docutils literal notranslate"><span class="pre">eval</span></code> methods will also check against some special values
with <code class="docutils literal notranslate"><span class="pre">==</span></code> but it is preferable to check e.g. <code class="docutils literal notranslate"><span class="pre">x.is_zero</span></code> rather than
<code class="docutils literal notranslate"><span class="pre">x==0</span></code>.</p>
<p>Note also that the <code class="docutils literal notranslate"><span class="pre">expreal.eval</span></code> method validates that the argument is
real. We want to allow <span class="math notranslate nohighlight">\(\pm\infty\)</span> as arguments to <code class="docutils literal notranslate"><span class="pre">expreal</span></code> so we check for
<code class="docutils literal notranslate"><span class="pre">extended_real</span></code> rather than <code class="docutils literal notranslate"><span class="pre">real</span></code>. If the argument is not extended real
then we raise an error:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="n">I</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">non-real argument to expreal</span>
</pre></div>
</div>
<p>Importantly we check <code class="docutils literal notranslate"><span class="pre">x.is_extended_real</span> <span class="pre">is</span> <span class="pre">False</span></code> rather than <code class="docutils literal notranslate"><span class="pre">not</span>
<span class="pre">x.is_extended_real</span></code> which means that we only reject the argument if it is
<em>definitely</em> not extended real: if <code class="docutils literal notranslate"><span class="pre">x.is_extended_real</span></code> gives <code class="docutils literal notranslate"><span class="pre">None</span></code> then
the argument will not be rejected. The first reason for allowing
<code class="docutils literal notranslate"><span class="pre">x.is_extended_real=None</span></code> is so that a vanilla symbol can be used with
<code class="docutils literal notranslate"><span class="pre">expreal</span></code>. The second reason is that an assumptions query can always give
<code class="docutils literal notranslate"><span class="pre">None</span></code> even in cases where an argument is definitely real e.g.:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">is_extended_real</span><span class="p">)</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">expreal(x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">I</span><span class="p">)</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">I</span><span class="p">)</span><span class="o">/</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">expr</span><span class="o">.</span><span class="n">is_extended_real</span><span class="p">)</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">expand</span><span class="p">()</span>
<span class="go">sqrt(2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expr</span><span class="o">.</span><span class="n">expand</span><span class="p">()</span><span class="o">.</span><span class="n">is_extended_real</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="n">expr</span><span class="p">)</span>
<span class="go">expreal(sqrt(2)*(1 - I)/2 + sqrt(2)*(1 + I)/2)</span>
</pre></div>
</div>
<p>Validating the argument in <code class="docutils literal notranslate"><span class="pre">expreal.eval</span></code> does mean that it will not be
validated when <code class="docutils literal notranslate"><span class="pre">evaluate=False</span></code> is passed but there is not really a better
place to perform the validation:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="n">I</span><span class="p">,</span> <span class="n">evaluate</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">expreal(I)</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">extended_nonnegative</span></code> class attribute and the <code class="docutils literal notranslate"><span class="pre">_eval_is_*</span></code> methods on
the <code class="docutils literal notranslate"><span class="pre">expreal</span></code> class implement queries in the assumptions system for
instances of <code class="docutils literal notranslate"><span class="pre">expreal</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">expreal(2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">is_finite</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">is_integer</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">is_rational</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">is_algebraic</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span> <span class="o">=</span> <span class="n">expreal</span><span class="p">(</span><span class="o">-</span><span class="n">oo</span><span class="p">,</span> <span class="n">evaluate</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span>
<span class="go">expreal(-oo)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">z</span><span class="o">.</span><span class="n">is_integer</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">real</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">expreal(x)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">expreal</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">is_nonnegative</span>
<span class="go">True</span>
</pre></div>
</div>
<p>The assumptions system resolves queries like <code class="docutils literal notranslate"><span class="pre">expreal(2).is_finite</span></code> using
the corresponding handler <code class="docutils literal notranslate"><span class="pre">expreal._eval_is_finite</span></code> and <em>also</em> the
implication rules. For example it is known that <code class="docutils literal notranslate"><span class="pre">expreal(2).is_rational</span></code> is
<code class="docutils literal notranslate"><span class="pre">False</span></code> because <code class="docutils literal notranslate"><span class="pre">expreal(2)._eval_is_algebraic</span></code> returns <code class="docutils literal notranslate"><span class="pre">False</span></code> and
there is an implication rule <code class="docutils literal notranslate"><span class="pre">rational</span> <span class="pre">-&gt;</span> <span class="pre">algebraic</span></code>. This means that an
<code class="docutils literal notranslate"><span class="pre">is_rational</span></code> query can be resolved in this case by the
<code class="docutils literal notranslate"><span class="pre">_eval_is_algebraic</span></code> handler. It is actually better not to implement
assumptions handlers for every possible predicate but rather to try and
identify a minimal set of handlers that can resolve as many queries as
possible with as few checks as possible.</p>
<p>Another point to note is that the <code class="docutils literal notranslate"><span class="pre">_eval_is_*</span></code> methods only make assumptions
queries on the argument <code class="docutils literal notranslate"><span class="pre">x</span></code> and do not make any assumptions queries on
<code class="docutils literal notranslate"><span class="pre">self</span></code>. Recursive assumptions queries on the same object will interfere with
the assumptions implications resolver potentially leading to non-deterministic
behaviour so they should not be used (there are examples of this in the SymPy
codebase but they should be removed).</p>
<p>Many of the <code class="docutils literal notranslate"><span class="pre">expreal</span></code> methods implicitly return <code class="docutils literal notranslate"><span class="pre">None</span></code>. This is a common
pattern in the assumptions system. The <code class="docutils literal notranslate"><span class="pre">eval</span></code> method and the <code class="docutils literal notranslate"><span class="pre">_eval_is_*</span></code>
methods can all return <code class="docutils literal notranslate"><span class="pre">None</span></code> and often will. A Python function that ends
without reaching a <code class="docutils literal notranslate"><span class="pre">return</span></code> statement will implicitly return <code class="docutils literal notranslate"><span class="pre">None</span></code>. We
take advantage of this by leaving out many of the <code class="docutils literal notranslate"><span class="pre">else</span></code> clauses from the
<code class="docutils literal notranslate"><span class="pre">if</span></code> statements and allowing <code class="docutils literal notranslate"><span class="pre">None</span></code> to be returned implicitly. When
following the control flow of these methods it is important to bear in mind
firstly that any queried property can give <code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code> or <code class="docutils literal notranslate"><span class="pre">None</span></code> and
also that any function will implicitly return <code class="docutils literal notranslate"><span class="pre">None</span></code> if all of the
conditionals fail.</p>
</section>
<section id="mechanism-of-the-assumptions-system">
<h2>Mechanism of the assumptions system<a class="headerlink" href="#mechanism-of-the-assumptions-system" title="Permalink to this headline">¶</a></h2>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>This section describes internal details that could change in a
future SymPy version.</p>
</div>
<p>This section will explain the inner workings of the assumptions system. It is
important to understand that these inner workings are implementation details
and could change from one SymPy version to another. This explanation is
written as of SymPy 1.7. Although the (old) assumptions system has many
limitations (discussed in the next section) it is a mature system that is used
extensively in SymPy and has been well optimised for its current usage. The
assumptions system is used implicitly in most SymPy operations to control
evaluation of elementary expressions.</p>
<p>There are several stages in the implementation of the assumptions system
within a SymPy process that lead up to the evaluation of a single query in the
assumptions system. Briefly these are:</p>
<ol class="arabic simple">
<li><p>At import time the assumptions rules defined in
<code class="docutils literal notranslate"><span class="pre">sympy/core/assumptions.py</span></code> are processed into a canonical form ready for
efficiently applying the implication rules. This happens once when SymPy is
imported before even the <a class="reference internal" href="../modules/core.html#sympy.core.basic.Basic" title="sympy.core.basic.Basic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Basic</span></code></a> class is defined.</p></li>
<li><p>The <code class="docutils literal notranslate"><span class="pre">ManagedProperties</span></code> metaclass is defined which is the metaclass for
all <a class="reference internal" href="../modules/core.html#sympy.core.basic.Basic" title="sympy.core.basic.Basic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Basic</span></code></a> subclasses. This class will post-process every
<a class="reference internal" href="../modules/core.html#sympy.core.basic.Basic" title="sympy.core.basic.Basic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Basic</span></code></a> subclass to add the relevant properties needed for
assumptions queries.  This also adds the <code class="docutils literal notranslate"><span class="pre">default_assumptions</span></code> attribute
to the class. This happens each time a <a class="reference internal" href="../modules/core.html#sympy.core.basic.Basic" title="sympy.core.basic.Basic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Basic</span></code></a> subclass is defined.</p></li>
<li><p>Every <a class="reference internal" href="../modules/core.html#sympy.core.basic.Basic" title="sympy.core.basic.Basic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Basic</span></code></a> instance initially uses the <code class="docutils literal notranslate"><span class="pre">default_assumptions</span></code> class
attribute. When an assumptions query is made on a <a class="reference internal" href="../modules/core.html#sympy.core.basic.Basic" title="sympy.core.basic.Basic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Basic</span></code></a> instance
in the first instance the query will be answered from the
<code class="docutils literal notranslate"><span class="pre">default_assumptions</span></code> for the class.</p></li>
<li><p>If there is no cached value for the assumptions query in the
<code class="docutils literal notranslate"><span class="pre">default_assumptions</span></code> for the class then the default assumptions will be
copied to make an assumptions cache for the instance. Then the <code class="docutils literal notranslate"><span class="pre">_ask()</span></code>
function is called to resolve the query which will firstly call the
relevant instance handler <code class="docutils literal notranslate"><span class="pre">_eval_is</span></code> method. If the handler returns
non-None then the result will be cached and returned.</p></li>
<li><p>If the handler does not exist or gives None then the implications resolver
is tried. This will enumerate (in a randomised order) all possible
combinations of predicates that could potentially be used to resolve the
query under the implication rules. In each case the handler <code class="docutils literal notranslate"><span class="pre">_eval_is</span></code>
method will be called to see if it gives non-None. If any combination of
handlers and implication rules leads to a definitive result for the query
then that result is cached in the instance cache and returned.</p></li>
<li><p>Finally if the implications resolver failed to resolve the query then the
query is considered unresolvable. The value of <code class="docutils literal notranslate"><span class="pre">None</span></code> for the query is
cached in the instance cache and returned.</p></li>
</ol>
<p>The assumptions rules defined in <code class="docutils literal notranslate"><span class="pre">sympy/core/assumptions.py</span></code> are given in
forms like <code class="docutils literal notranslate"><span class="pre">real</span> <span class="pre">==</span>&#160; <span class="pre">negative</span> <span class="pre">|</span> <span class="pre">zero</span> <span class="pre">|</span> <span class="pre">positive</span></code>. When this module is
imported these are converted into a <code class="docutils literal notranslate"><span class="pre">FactRules</span></code> instance called
<code class="docutils literal notranslate"><span class="pre">_assume_rules</span></code>. This preprocesses the implication rules into the form of
“A” and “B” rules that can be used for the implications resolver. This is
explained in the code in <code class="docutils literal notranslate"><span class="pre">sympy/core/facts.py</span></code>. We can access this internal
object directly like (full output omitted):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.core.assumptions</span> <span class="kn">import</span> <span class="n">_assume_rules</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">_assume_rules</span><span class="o">.</span><span class="n">defined_facts</span>   
<span class="go">{&#39;algebraic&#39;,</span>
<span class="go"> &#39;antihermitian&#39;,</span>
<span class="go"> &#39;commutative&#39;,</span>
<span class="go"> &#39;complex&#39;,</span>
<span class="go"> &#39;composite&#39;,</span>
<span class="go"> &#39;even&#39;,</span>
<span class="go"> ...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">_assume_rules</span><span class="o">.</span><span class="n">full_implications</span>   
<span class="go">defaultdict(set,</span>
<span class="go">            {(&#39;extended_positive&#39;, False): {(&#39;composite&#39;, False),</span>
<span class="go">  (&#39;positive&#39;, False),</span>
<span class="go">  (&#39;prime&#39;, False)},</span>
<span class="go"> (&#39;finite&#39;, False): {(&#39;algebraic&#39;, False),</span>
<span class="go">  (&#39;complex&#39;, False),</span>
<span class="go">  (&#39;composite&#39;, False),</span>
<span class="go">  ...</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">ManagedProperties</span></code> metaclass will inspect the attributes of each
<code class="docutils literal notranslate"><span class="pre">Basic</span></code> class to see if any assumptions related attributes are defined. An
example of these is the <code class="docutils literal notranslate"><span class="pre">is_extended_nonnegative</span> <span class="pre">=</span> <span class="pre">True</span></code> attribute defined
in the <code class="docutils literal notranslate"><span class="pre">expreal</span></code> class. The implications of any such attributes will be
used to precompute any statically knowable assumptions. For example
<code class="docutils literal notranslate"><span class="pre">is_extended_nonnegative=True</span></code> implies <code class="docutils literal notranslate"><span class="pre">real=True</span></code> etc. A <code class="docutils literal notranslate"><span class="pre">StdFactKB</span></code>
instance is created for the class which stores those assumptions whose values
are known at this stage. The <code class="docutils literal notranslate"><span class="pre">StdFactKB</span></code> instance is assigned as the class
attribute <code class="docutils literal notranslate"><span class="pre">default_assumptions</span></code>. We can see this with</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Expr</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="n">Expr</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">is_positive</span> <span class="o">=</span> <span class="kc">True</span>
<span class="gp">...</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">_eval_is_rational</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="c1"># Let&#39;s print something to see when this method is called...</span>
<span class="gp">... </span>        <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;!!! calling _eval_is_rational&#39;</span><span class="p">)</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="kc">True</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="o">.</span><span class="n">is_positive</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="o">.</span><span class="n">is_real</span>  <span class="c1"># inferred from is_positive</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Although only <code class="docutils literal notranslate"><span class="pre">is_positive</span></code> was defined in the class <code class="docutils literal notranslate"><span class="pre">A</span></code> it also has
attributes such as <code class="docutils literal notranslate"><span class="pre">is_real</span></code> which are inferred from <code class="docutils literal notranslate"><span class="pre">is_positive</span></code>. The
set of all such assumptions for class <code class="docutils literal notranslate"><span class="pre">A</span></code> can be seen in
<code class="docutils literal notranslate"><span class="pre">default_assumptions</span></code> which looks like a <code class="docutils literal notranslate"><span class="pre">dict</span></code> but is in fact a
<code class="docutils literal notranslate"><span class="pre">StdFactKB</span></code> instance:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">A</span><span class="o">.</span><span class="n">default_assumptions</span><span class="p">)</span>
<span class="go">&lt;class &#39;sympy.core.assumptions.StdFactKB&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="o">.</span><span class="n">default_assumptions</span>
<span class="go">{&#39;commutative&#39;: True,</span>
<span class="go"> &#39;complex&#39;: True,</span>
<span class="go"> &#39;extended_negative&#39;: False,</span>
<span class="go"> &#39;extended_nonnegative&#39;: True,</span>
<span class="go"> &#39;extended_nonpositive&#39;: False,</span>
<span class="go"> &#39;extended_nonzero&#39;: True,</span>
<span class="go"> &#39;extended_positive&#39;: True,</span>
<span class="go"> &#39;extended_real&#39;: True,</span>
<span class="go"> &#39;finite&#39;: True,</span>
<span class="go"> &#39;hermitian&#39;: True,</span>
<span class="go"> &#39;imaginary&#39;: False,</span>
<span class="go"> &#39;infinite&#39;: False,</span>
<span class="go"> &#39;negative&#39;: False,</span>
<span class="go"> &#39;nonnegative&#39;: True,</span>
<span class="go"> &#39;nonpositive&#39;: False,</span>
<span class="go"> &#39;nonzero&#39;: True,</span>
<span class="go"> &#39;positive&#39;: True,</span>
<span class="go"> &#39;real&#39;: True,</span>
<span class="go"> &#39;zero&#39;: False}</span>
</pre></div>
</div>
<p>When an instance of any <a class="reference internal" href="../modules/core.html#sympy.core.basic.Basic" title="sympy.core.basic.Basic"><code class="xref py py-class docutils literal notranslate"><span class="pre">Basic</span></code></a> subclass is created <code class="docutils literal notranslate"><span class="pre">Basic.__new__</span></code> will
assign its <code class="docutils literal notranslate"><span class="pre">_assumptions</span></code> attribute which will initially be a reference to
<code class="docutils literal notranslate"><span class="pre">cls.default_assumptions</span></code> shared amongst all instances of the same class.
The instance will use this to resolve any assumptions queries until that fails
to give a definitive result at which point a copy of
<code class="docutils literal notranslate"><span class="pre">cls.default_assumptions</span></code> will be created and assigned to the instance’s
<code class="docutils literal notranslate"><span class="pre">_assumptions</span></code> attribute. The copy will be used as a cache to store any
results computed for the instance by its <code class="docutils literal notranslate"><span class="pre">_eval_is</span></code> handlers.</p>
<p>When the <code class="docutils literal notranslate"><span class="pre">_assumptions</span></code> attribute fails to give the relevant result it is
time to call the <code class="docutils literal notranslate"><span class="pre">_eval_is</span></code> handlers. At this point the <code class="docutils literal notranslate"><span class="pre">_ask()</span></code> function
is called. The <code class="docutils literal notranslate"><span class="pre">_ask()</span></code> function will initially try to resolve a query such
as <code class="docutils literal notranslate"><span class="pre">is_rational</span></code> by calling the corresponding method i.e.
<code class="docutils literal notranslate"><span class="pre">_eval_is_rational</span></code>. If that gives non-None then the result is stored in
<code class="docutils literal notranslate"><span class="pre">_assumptions</span></code> and any implications of that result are computed and stored
as well. At that point the query is resolved and the value returned.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">_assumptions</span> <span class="ow">is</span> <span class="n">A</span><span class="o">.</span><span class="n">default_assumptions</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">is_rational</span>
<span class="go">!!! calling _eval_is_rational</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">_assumptions</span> <span class="ow">is</span> <span class="n">A</span><span class="o">.</span><span class="n">default_assumptions</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">_assumptions</span>   <span class="c1"># rational now shows as True</span>
<span class="go">{&#39;algebraic&#39;: True,</span>
<span class="go"> &#39;commutative&#39;: True,</span>
<span class="go"> &#39;complex&#39;: True,</span>
<span class="go"> &#39;extended_negative&#39;: False,</span>
<span class="go"> &#39;extended_nonnegative&#39;: True,</span>
<span class="go"> &#39;extended_nonpositive&#39;: False,</span>
<span class="go"> &#39;extended_nonzero&#39;: True,</span>
<span class="go"> &#39;extended_positive&#39;: True,</span>
<span class="go"> &#39;extended_real&#39;: True,</span>
<span class="go"> &#39;finite&#39;: True,</span>
<span class="go"> &#39;hermitian&#39;: True,</span>
<span class="go"> &#39;imaginary&#39;: False,</span>
<span class="go"> &#39;infinite&#39;: False,</span>
<span class="go"> &#39;irrational&#39;: False,</span>
<span class="go"> &#39;negative&#39;: False,</span>
<span class="go"> &#39;nonnegative&#39;: True,</span>
<span class="go"> &#39;nonpositive&#39;: False,</span>
<span class="go"> &#39;nonzero&#39;: True,</span>
<span class="go"> &#39;positive&#39;: True,</span>
<span class="go"> &#39;rational&#39;: True,</span>
<span class="go"> &#39;real&#39;: True,</span>
<span class="go"> &#39;transcendental&#39;: False,</span>
<span class="go"> &#39;zero&#39;: False}</span>
</pre></div>
</div>
<p>If e.g. <code class="docutils literal notranslate"><span class="pre">_eval_is_rational</span></code> does not exist or gives <code class="docutils literal notranslate"><span class="pre">None</span></code> then <code class="docutils literal notranslate"><span class="pre">_ask()</span></code>
will try all possibilities to use the implication rules and any other handler
methods such as <code class="docutils literal notranslate"><span class="pre">_eval_is_integer</span></code>, <code class="docutils literal notranslate"><span class="pre">_eval_is_algebraic</span></code> etc that might
possibly be able to give an answer to the original query. If any method leads
to a definite result being known for the original query then that is returned.
Otherwise once all possibilities for using a handler and the implication rules
to resolve the query are exhausted <code class="docutils literal notranslate"><span class="pre">None</span></code> will be cached and returned.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">is_algebraic</span>    <span class="c1"># called _eval_is_rational indirectly</span>
<span class="go">!!! calling _eval_is_rational</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">is_prime</span><span class="p">)</span>   <span class="c1"># called _eval_is_rational indirectly</span>
<span class="go">!!! calling _eval_is_rational</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">_assumptions</span>   <span class="c1"># prime now shows as None</span>
<span class="go">{&#39;algebraic&#39;: True,</span>
<span class="go"> &#39;commutative&#39;: True,</span>
<span class="go"> &#39;complex&#39;: True,</span>
<span class="go"> &#39;extended_negative&#39;: False,</span>
<span class="go"> &#39;extended_nonnegative&#39;: True,</span>
<span class="go"> &#39;extended_nonpositive&#39;: False,</span>
<span class="go"> &#39;extended_nonzero&#39;: True,</span>
<span class="go"> &#39;extended_positive&#39;: True,</span>
<span class="go"> &#39;extended_real&#39;: True,</span>
<span class="go"> &#39;finite&#39;: True,</span>
<span class="go"> &#39;hermitian&#39;: True,</span>
<span class="go"> &#39;imaginary&#39;: False,</span>
<span class="go"> &#39;infinite&#39;: False,</span>
<span class="go"> &#39;irrational&#39;: False,</span>
<span class="go"> &#39;negative&#39;: False,</span>
<span class="go"> &#39;nonnegative&#39;: True,</span>
<span class="go"> &#39;nonpositive&#39;: False,</span>
<span class="go"> &#39;nonzero&#39;: True,</span>
<span class="go"> &#39;positive&#39;: True,</span>
<span class="go"> &#39;prime&#39;: None,</span>
<span class="go"> &#39;rational&#39;: True,</span>
<span class="go"> &#39;real&#39;: True,</span>
<span class="go"> &#39;transcendental&#39;: False,</span>
<span class="go"> &#39;zero&#39;: False}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>In the <code class="docutils literal notranslate"><span class="pre">_ask()</span></code> function the handlers are called in a randomised
order which can mean that execution at this point is
non-deterministic. Provided all of the different handler methods
are consistent (i.e. there are no bugs) then the end result will
still be deterministic. However a bug where two handlers are
inconsistent can manifest in non-deterministic behaviour because
this randomisation might lead to the handlers being called in
different orders when the same program is run multiple times.</p>
</div>
</section>
<section id="limitations">
<h2>Limitations<a class="headerlink" href="#limitations" title="Permalink to this headline">¶</a></h2>
<section id="combining-predicates-with-or">
<h3>Combining predicates with or<a class="headerlink" href="#combining-predicates-with-or" title="Permalink to this headline">¶</a></h3>
<p>In the old assumptions we can easily combine predicates with <em>and</em> when
creating a Symbol e.g.:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">,</span> <span class="n">integer</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">positive</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">is_positive</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">is_integer</span>
<span class="go">True</span>
</pre></div>
</div>
<p>We can also easily query whether two conditions are jointly satisfied with</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fuzzy_and</span><span class="p">([</span><span class="n">x</span><span class="o">.</span><span class="n">is_positive</span><span class="p">,</span> <span class="n">x</span><span class="o">.</span><span class="n">is_integer</span><span class="p">])</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">.</span><span class="n">is_positive</span> <span class="ow">and</span> <span class="n">x</span><span class="o">.</span><span class="n">is_integer</span>
<span class="go">True</span>
</pre></div>
</div>
<p>However there is no way in the old assumptions to create a <a class="reference internal" href="../modules/core.html#sympy.core.symbol.Symbol" title="sympy.core.symbol.Symbol"><code class="xref py py-class docutils literal notranslate"><span class="pre">Symbol</span></code></a>
with assumptions predicates combined with <em>or</em>. For example if we wanted to
say that “x is positive or x is an integer” then it is not possible to create
a <a class="reference internal" href="../modules/core.html#sympy.core.symbol.Symbol" title="sympy.core.symbol.Symbol"><code class="xref py py-class docutils literal notranslate"><span class="pre">Symbol</span></code></a> with those assumptions.</p>
<p>It is also not possible to ask an assumptions query based on <em>or</em> e.g. “is
expr an expression that is positive or an integer”. We can use e.g.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fuzzy_or</span><span class="p">([</span><span class="n">x</span><span class="o">.</span><span class="n">is_positive</span><span class="p">,</span> <span class="n">x</span><span class="o">.</span><span class="n">is_integer</span><span class="p">])</span>
<span class="go">True</span>
</pre></div>
</div>
<p>However if all that is known about <code class="docutils literal notranslate"><span class="pre">x</span></code> is that it is possibly positive or
otherwise a negative integer then both queries <code class="docutils literal notranslate"><span class="pre">x.is_positive</span></code> and
<code class="docutils literal notranslate"><span class="pre">x.is_integer</span></code> will resolve to <code class="docutils literal notranslate"><span class="pre">None</span></code>.  That means that the query becomes</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fuzzy_or</span><span class="p">([</span><span class="kc">None</span><span class="p">,</span> <span class="kc">None</span><span class="p">])</span>
</pre></div>
</div>
<p>which then also gives <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
</section>
<section id="relations-between-different-symbols">
<h3>Relations between different symbols<a class="headerlink" href="#relations-between-different-symbols" title="Permalink to this headline">¶</a></h3>
<p>A fundamental limitation of the old assumptions system is that all explicit
assumptions are properties of an individual symbol. There is no way in this
system to make an assumption about the <em>relationship</em> between two symbols. One
of the most common requests is the ability to assume something like <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span></code>
but there is no way to even specify that in the old assumptions.</p>
<p>The new assumptions have the theoretical capability that relational
assumptions can be specified. However the algorithms to make use of that
information are not yet implemented and the exact API for specifying
relational assumptions has not been decided upon.</p>
</section>
<section id="dynamic-changing-assumptions">
<h3>Dynamic changing assumptions<a class="headerlink" href="#dynamic-changing-assumptions" title="Permalink to this headline">¶</a></h3>
</section>
<section id="selectively-controlling-evaluation">
<h3>Selectively controlling evaluation<a class="headerlink" href="#selectively-controlling-evaluation" title="Permalink to this headline">¶</a></h3>
</section>
<section id="extensibility">
<h3>Extensibility<a class="headerlink" href="#extensibility" title="Permalink to this headline">¶</a></h3>
</section>
</section>
<section id="new-assumptions">
<h2>New assumptions<a class="headerlink" href="#new-assumptions" title="Permalink to this headline">¶</a></h2>
<p>ZZZ: Talk about the new assumptions here…</p>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Assumptions</a><ul>
<li><a class="reference internal" href="#the-old-assumptions-system">The (old) assumptions system</a></li>
<li><a class="reference internal" href="#gotcha-symbols-with-different-assumptions">Gotcha: symbols with different assumptions</a></li>
<li><a class="reference internal" href="#applying-assumptions-to-string-inputs">Applying assumptions to string inputs</a></li>
<li><a class="reference internal" href="#predicates">Predicates</a><ul>
<li><a class="reference internal" href="#references-for-the-above-definitions">References for the above definitions</a></li>
</ul>
</li>
<li><a class="reference internal" href="#implications">Implications</a></li>
<li><a class="reference internal" href="#interpretation-of-the-predicates">Interpretation of the predicates</a></li>
<li><a class="reference internal" href="#other-is-properties">Other is_* properties</a></li>
<li><a class="reference internal" href="#implementing-assumptions-handlers">Implementing assumptions handlers</a></li>
<li><a class="reference internal" href="#mechanism-of-the-assumptions-system">Mechanism of the assumptions system</a></li>
<li><a class="reference internal" href="#limitations">Limitations</a><ul>
<li><a class="reference internal" href="#combining-predicates-with-or">Combining predicates with or</a></li>
<li><a class="reference internal" href="#relations-between-different-symbols">Relations between different symbols</a></li>
<li><a class="reference internal" href="#dynamic-changing-assumptions">Dynamic changing assumptions</a></li>
<li><a class="reference internal" href="#selectively-controlling-evaluation">Selectively controlling evaluation</a></li>
<li><a class="reference internal" href="#extensibility">Extensibility</a></li>
</ul>
</li>
<li><a class="reference internal" href="#new-assumptions">New assumptions</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="index.html"
                        title="previous chapter">SymPy Guides</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="booleans.html"
                        title="next chapter">Symbolic and fuzzy booleans</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/guides/assumptions.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="booleans.html" title="Symbolic and fuzzy booleans"
             >next</a> |</li>
        <li class="right" >
          <a href="index.html" title="SymPy Guides"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >SymPy Guides</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Assumptions</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/guides/assumptions.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:00 GMT -->
</html>